package com.reptile.lottery.service;


import com.reptile.lottery.dao.SportsLottoDao;
import com.reptile.lottery.dao.TwoColorBallDao;
import com.reptile.lottery.show.SportsLotto;
import com.reptile.lottery.show.TwoColorBall;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import net.htmlparser.jericho.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Description:
 * Author:zhouyongqiang
 * Date: 2020/12/22 15:33
 * Copyright (c) 2020,All Rights Reserved.
 */
@Service
public class SportsLottoService {
    @Autowired
    private SportsLottoDao sportsLottoDao;

    public List<SportsLotto> getAllSportsLottos() {
        return sportsLottoDao.findAll();
    }

    public void searchAllSportsLotto() {
        OkHttpClient client = new OkHttpClient();
        String url = "https://kaijiang.500.com/dlt.shtml";
        String content = null;
        try {
            content = client.newCall(new Request.Builder().url(url).get().build()).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Source iSelectBox = new Source(content);
        Element table = iSelectBox.getFirstElementByClass("iSelectBox");
        Element selectBoxs = table.getFirstElement(HTMLElementName.SPAN).getFirstElement();

        Source iSelectSource = new Source(selectBoxs.toString());
        Element iSelectTable = iSelectSource.getFirstElementByClass("iSelectList");
        Element selectList = iSelectTable.getFirstElement(HTMLElementName.DIV).getFirstElement();

        Source hrefSource = new Source(selectList.toString());
        List<Element> hrefList = hrefSource.getAllElements(HTMLElementName.A);
        for (int i = 0; i < hrefList.size(); i++) {
            Element hrefElement = hrefList.get(i);
            Integer number = Integer.valueOf(hrefElement.getContent().toString());
            String href = hrefElement.getAttributeValue("href");
            SportsLotto sportsLotto = sportsLottoDao.findByNumber(number);
            if (sportsLotto == null) {
                addNumber(number, href);
            }
        }
    }

    public void addNumber(Integer number, String href) {
        OkHttpClient client = new OkHttpClient();
        String url = href;
        String content = null;
        try {
            content = client.newCall(new Request.Builder().url(url).get().build()).execute().body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Source isContent = new Source(content);
        Element ballBoxTable = isContent.getFirstElementByClass("ball_box01");
        Element ballBox = ballBoxTable.getFirstElement(HTMLElementName.DIV).getFirstElement();
        Element ul = ballBox.getFirstElement(HTMLElementName.UL).getFirstElement();
        List<Element> allElements = ul.getAllElements(HTMLElementName.LI);
        for (int i = 0; i < 7; i++) {
            String red1 = allElements.get(0).getContent().toString();
            String red2 = allElements.get(1).getContent().toString();
            String red3 = allElements.get(2).getContent().toString();
            String red4 = allElements.get(3).getContent().toString();
            String red5 = allElements.get(4).getContent().toString();
            String blue1 = allElements.get(5).getContent().toString();
            String blue2 = allElements.get(6).getContent().toString();
            SportsLotto sportsLotto = new SportsLotto();
            sportsLotto.setNumber(number);
            sportsLotto.setRed1(red1);
            sportsLotto.setRed2(red2);
            sportsLotto.setRed3(red3);
            sportsLotto.setRed4(red4);
            sportsLotto.setRed5(red5);
            sportsLotto.setBlue1(blue1);
            sportsLotto.setBlue2(blue2);
            sportsLottoDao.save(sportsLotto);
        }
    }

    public static void main(String[] args) {
        //searchAllSportsLotto();
        //addNumber(7001, "https://kaijiang.500.com/shtml/dlt/07001.shtml");
        System.out.println();
    }
}
